<template>
  <div class="cc-home-nav-container">
    <div class="cc-home-nav-container-logo">
      <div class="cc-home-nav-container-logo-text">
        CC
      </div>
      <div class="cc-home-nav-container-menu">
        <div v-for="(item, index) in navItems" class="cc-home-nav-container-menu-item" :key="`nav${index}`" @click="changeMenu(item.routeName)">
          {{item.label}}
        </div>
      </div>
    </div>
    <div class="cc-home-nav-container-right">
      <el-dropdown @command="changeDropDownMenu">
        <el-button :type="'text'" class="cc-home-nav-container-right-user pointer">
          {{ user.userNickName }}<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item v-for="(item, key) in dropDownMenu" :key="`dropdown${key}`" :command="item.command">{{ item.name }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script lang="ts">
import {computed, defineComponent, reactive} from 'vue';
import {useStore} from 'vuex';
import {DropDownMenuItem, MenuItem, MoreCommandEnum} from "@/common-class/system-class/Nav";
import {Wind3User} from "@/common-class/model/Wind3User";
import {logout} from "@/function-ts/UserFunction";
import router from "@/router";
import {RouteNameEnum} from "@/common-class/common-config/RouteClass";

export default defineComponent({
  name: 'HomeNav',
  setup() {
    let dropDownMenu = reactive<DropDownMenuItem[]>([
      {name: '切换用户', command: MoreCommandEnum.commandChangeUser},
      {name: '个人中心', command: MoreCommandEnum.commandPersonalCenter},
      {name: '注销登录', command: MoreCommandEnum.commandLogout}]);
    let changeDropDownMenu: (command: MoreCommandEnum) => void = (command: MoreCommandEnum) => {
      if (MoreCommandEnum.commandLogout === command) {
        logout();
      } else if (MoreCommandEnum.commandPersonalCenter === command) {
        router.push({name: RouteNameEnum.personalCenter})
      } else if (MoreCommandEnum.commandChangeUser === command) {
        router.push({name: RouteNameEnum.login})
      }
    }
    let navItems = reactive<MenuItem[]>([
      { label: '首页', routeName: RouteNameEnum.dashboard },
      { label: '管理区', routeName: RouteNameEnum.manager },
      { label: '测试区', routeName: RouteNameEnum.test }
    ])
    let changeMenu: (routeName: RouteNameEnum) => void = (routeName: RouteNameEnum) => {
      router.push({name: routeName})
    }
    let user = computed((): Wind3User => useStore().getters['user/user'] ?? reactive(new Wind3User()));
    return {user, dropDownMenu, changeDropDownMenu, navItems, changeMenu};
  }
});

</script>

<style lang="scss" scoped>
.cc-home-nav {
  &-container {
    background: #001938;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;

    &-logo {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;

      &-text {
        padding-left: 20px;
        font-size: 22px;
        line-height: 25px;
        color: #ffffff;
      }
    }

    &-menu{
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
      padding-right: 20px;
      &-item{
        color: #FFFFFF;
        text-align: center;
        font-size: 14px;
        width: 80px;
      }
      &-item:hover{
        font-size: 16px;
        cursor: pointer;
      }
    }

    &-right {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      padding-right: 20px;

      &-user {
        font-size: 18px;
        line-height: 40px;
        color: #ffffff;
        font-weight: 600;
      }
    }
  }
}
</style>
